﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using zbus;
using System.Threading;

namespace GtaAPIClient
{
    public partial class ServiceConsole : Form
    {  
        
        public ServiceConsole()
        {
            InitializeComponent();
        }

        Thread thread1;
        public delegate void UpdateTextDelegate(string str);


        public void updateText(string str)
        {
            if (textBox1.InvokeRequired)
            {
                UpdateTextDelegate outDelegate = new UpdateTextDelegate(updateText);
                Invoke(outDelegate, new object[] { str });
            }
            else
            {
                if (textBox1.TextLength > 30000)
                {
                    textBox1.Clear();
                }
                textBox1.AppendText(DateTime.Now + " " + str);
                textBox1.AppendText("\n");
            }
        }


        private ProcClass threadProc = null;
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {  
                threadProc = new ProcClass("", new UpdateTextDelegate(updateText));
                thread1 = new Thread(new ThreadStart(threadProc.startThread));
                thread1.IsBackground = true;
                thread1.Start();
                button1.Enabled = false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        { 
            if (thread1 != null)
            {
                textBox1.AppendText("正在关闭服务...\n"); 
                thread1.Abort();
                thread1.Join();
                if (threadProc != null)
                {
                    threadProc.Dispose();
                    threadProc = null;
                }
                textBox1.AppendText("服务已关闭...\n"); 
                button1.Enabled = true;
            }
        }
         

        /// <summary>
        /// test按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            //测试连接代码
            //try
            //{
            //    AccountManage am = new AccountManage(new UpdateTextDelegate(updateText));
            //    //am.openAccount("", "admin", "pwhWQFzLWcw=","","", 100000, 0, 0);
            //    //am.fundTransfer(10259, 1, 2, 100000, 1);
            //    string msg = "";
            //    object obj = am.findAccount(out msg, "10260", "fzjQ37ynndk=");

            //    textBox1.AppendText("测试国泰安接口连接成功...\n");
            //}
            //catch (Exception ex)
            //{
            //    throw new Exception("国泰安接口连接失败", ex);
            //}  
            

            //zbus客户端代码 
            //RpcConfig config = new RpcConfig();
            //config.Service = "gta_srv";
            //config.Host = "172.24.180.27";
            //config.Timeout = 100000;
            //config.Port = 15555;
            //Rpc rpc = new Rpc(config);
            ////object[] args = new object[]{"", "10260", "fzjQ37ynndk=" };
            ////object obj = rpc.Invoke("findAccount", args);

            //object[] args = new object[]{"iap_kaihu7", "iap_kaihu7", "123456", "iap_kaihu7", "123456", "10000", "0", "0" };
            //object openAccount = rpc.Invoke("findAccount", args);
            //textBox1.AppendText("测试zbus连接成功...\n"); 
            

            //打开测试界面
            TestForm tf = new TestForm();
            tf.Show();
             
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        } 
        
    }

    public class ProcClass : IDisposable
    {
        private string procParameter = "";
        private ServiceConsole.UpdateTextDelegate delg = null;
        //HONG LEIMING added 20150619
        private List<Broker> brokers = null;
        private Service zbusSerivce = null;
        
        public ProcClass(string parameter, ServiceConsole.UpdateTextDelegate delg)
        {
            procParameter = parameter;
            this.delg = delg;
        }

        public void startThread()
        {
            AccountManage asrv = new AccountManage(delg);
            XhManage xhsrv = new XhManage(delg);
            RzrjManage rzrjsrv = new RzrjManage(delg);
            IfManage ifsrv = new IfManage(delg);

            Config cfg = new Config();

            string[] brokersAddress = cfg.brokers;
            string serviceName = cfg.serverNmae;

            this.brokers = new List<Broker>(); 
            foreach(string brokerAddress in brokersAddress)
            {
                SingleBrokerConfig config = new SingleBrokerConfig();
                config.brokerAddress = brokerAddress;
                Broker broker = new SingleBroker(config);
                this.brokers.Add(broker);
            }

            RpcServiceHandler handler = new RpcServiceHandler(asrv, xhsrv, rzrjsrv, ifsrv);

            ServiceConfig serviceConfig = new ServiceConfig(brokers.ToArray());
            serviceConfig.consumerCount = cfg.threadNum;
            serviceConfig.mq = serviceName;
            serviceConfig.serviceHandler = handler;

            this.zbusSerivce = new Service(serviceConfig);
            this.zbusSerivce.Start();

            //Invoke(new UpdateTextDelegate(updateText), new object[] {"服务启动成功..."});
            delg.BeginInvoke("服务启动成功...", null, null);
        }

        public void Dispose()
        {
            if (this.zbusSerivce != null)
            {
                this.zbusSerivce.Dispose();
                this.zbusSerivce = null;
            }
            if (this.brokers != null)
            {
                foreach (Broker broker in this.brokers)
                {
                    broker.Dispose();
                }
            }
        }
    }
}
